<template>
    <div class="Business" style="width: 100%;">
        <Form ref="formData" :model="formData" :rules="formRules" :label-width="240" :disabled="this.readOnly">
            <Row>
                <Col :xs="24" :sm="12"></Col>
            </Row>
            <Row>
                <Col :xs="24" :sm="24">
                    <!--                    <Tabs v-model="this.verSion" type="card" @on-click="showContract"  ref="tabs" >-->
                    <!--                        <TabPane label="下载过的合同" name="newVersion" :disabled="this.readRadio">-->

                    <div style="width: 100%;">
                        <span>
                            <h2>激活时候的合同版本</h2>
                        </span>
                        <Table

                            no-data-text="暂无历史数据"
                            :row-class-name="rowClassName"
                            :columns="columns5"
                            :data="newVersionContractReissurVoOld"
                            highlight-row
                        >
                            <template slot-scope="{ row }" slot="versionList">
                                <Select :value="row.versionList[0].versionId" placeholder disabled transfer>
                                    <Option v-for="(item,index) in  row.versionList" :key="index" :value="item.versionId">
                                        {{item.versionId}}
                                    </Option>
                                </Select>
                            </template>

                        </Table>

                        <div class="business-btns">
                            <Button type="primary" size="small" @click="showModal2" v-if="!this.formData.isSP" >增加合同</Button>
                            <!--                                <Button type="primary" size="small" @click="printContractNow()"  v-if="!this.isSP">生成合同</Button>-->
                        </div>
                        <div v-if="!this.formData.isSP">
                    <Table

                        ref="selection"
                        @on-selection-change="onSelect"
                        no-data-text="暂无数据"
                        :row-class-name="rowClassName"
                        :columns="columns4"
                        :data="newVersionContractReissurVo"
                        highlight-row
                    >
                        <template slot-scope="{ row }" slot="versionList">
                            <Select :value="row.versionList[0].versionId" placeholder disabled transfer>
                                <Option v-for="(item,index) in  row.versionList" :key="index" :value="item.versionId">
                                    {{item.versionId}}
                                </Option>
                            </Select>
                        </template>
<!--                        <template slot-scope="{ row,index }" v-if="!row.isSP" slot="action">-->
<!--                            <Button type="error" size="small" @click="remove('new',index)"  >-->
<!--                                删除-->
<!--                            </Button>-->
<!--                        </template>-->

                    </Table>
                        </div>
                    </div>
                    <!--                        </TabPane>-->
                    <!--                        <TabPane label="其他合同" name="otherVersion"  :disabled="this.readRadio">-->
                    <!--                            <div class="business-btns">-->
                    <!--                                <Button type="primary" size="small" @click="showModal" >添加合同</Button>-->
                    <!--                            </div>-->
                    <!--                            <Table-->
                    <!--                                ref="selection"-->
                    <!--                                @on-selection-change="onSelect"-->
                    <!--                                no-data-text="暂无数据"-->
                    <!--                                :row-class-name="rowClassName"-->
                    <!--                                :columns="columns4"-->
                    <!--                                :data="otherVersionContractReissurVo"-->
                    <!--                                highlight-row-->
                    <!--                            >-->
                    <!--                                <template  slot-scope="{ row,index }" slot="action">-->
                    <!--                                    <Button type="error" size="small" @click="remove('other',index)">-->
                    <!--                                        删除-->
                    <!--                                    </Button>-->
                    <!--                                </template>-->
                    <!--                                <template slot-scope="{ row }" slot="versionList">-->
                    <!--                                    <Select :value="row.versionList[0].versionId" placeholder disabled transfer>-->
                    <!--                                        <Option v-for="(item,index) in  row.versionList" :key="index" :value="item.versionId">{{item.versionId}}</Option>-->
                    <!--                                    </Select>-->
                    <!--                                </template>-->
                    <!--                            </Table>-->
                    <!--                        </TabPane>-->
                    <!--                    </Tabs>-->

                </Col>
            </Row>
            <Row>
                <Col :xs="24" style="padding-top:16px;">
                    <FormItem label="简述原因" prop="remarks" disabled>
                        <Input type="textarea" v-model="formData.remarks"/>
                    </FormItem>
                </Col>
                <Col :xs="24">
                    <FormItem label="处理意见" prop="processReason" v-if="this.formData.processStatus!=='draft'&&this.formData.processStatus!=='toBeRepaired'&&this.formData.processStatus!==undefined" >
                        <Input type="text" v-model="formData.processReason" :disabled="this.formData.processStatus!=='underReview'&&this.formData.processStatus!=='repairedUnderReview'&&readOnly"/>
                    </FormItem>
                </Col>
            </Row>
        </Form>
        <addContract
            :ContractReissurVo2="ContractReissurVo2"
            :isModalOpen="isModalOpen"
            modalTitle="增加合同"
            @closeModal="closeModal"
            :contractNo="formData.contractNo"
            ref="addContract"
            @addContract="addContractFile"
        />
        <Spin size="large" fix v-if="spinShow"></Spin>
    </div>
</template>
<script>
    import addContract from "./addContract";
    import {
        queryContract,
        queryNewContract,
        queryContractByScene,
        printContract,
        queryBusinessContract,
        // queryContractInfo
    } from "_p/basic/api/contract/contract-reissue/index";

    export default {
        components: {
            addContract,
        },
        props: {
            dataDict: {
                type: Object,
                required: true
            },
            // 是否是供应商
            isSP: {
                type: Boolean,
                required: true
            },
            readOnly: {
                type: Boolean,
                require: true
            },
            //对象参数包含：合同号码、角色类型
            Obj: {
                type: Object,
                required: true
            },
            pageParams: {
                type: Object
            }
        },
        data() {
            return {
                children: "",
                indeterminate: false,
                checkAllType: false,
                readRadio: '',
                processStatusTem: "",
                clockModal: '',
                isSave: false,
                type: "",
                id: '',
                spinShow: false,
                changeSubject: "",
                business: "",
                openModal: false,
                isModalOpen: false,
                // verSion:'newVersion',
                verSion: "newVersion",

                modal: false,

                selectedCardStatus: 'row.versionId',
                selectedCardStatusTwo: [
                    {}
                ],
                printFormName: "",
                versionId: '0',
                versionList: [],
                showForm: {
                    type: "",
                    phone: false,
                    address: false,
                    parentTitle: "",
                    children: "",
                },
                formData: {
                    contractVersion: '',
                    remarks: '',
                    printNode: 'printLoanApply',
                    belongNo: '',
                    contractNo: '',
                    processStatus: '',
                    processReason:'',
                    isSP:'',
                },
                newVersionContractReissurVo: [],
                newVersionContractReissurVoOld:[],
                otherVersionContractReissurVo: [],
                ContractReissurVo: [
                    {
                        printNode: 'printLoanApply',
                        belongNo: '',
                        printFormName: '',
                        versionList: [
                            {
                                id: '',
                                versionId: 'this.versionId',
                            }
                        ]
                    }
                ],
                ContractReissurVo2: [
                    {
                        printNode: 'printLoanApply',
                        belongNo: '',
                        printFormName: '',
                        versionList: [
                            {
                                id: '',
                                versionId: 'this.versionId',
                            }
                        ]
                    }
                ],
                formRules: {
                    remarks: [{required: true, message: "请输入原因", trigger: "blur"}],


                },
                selecteds: [],
                version: {},
                columns4: [
                    {
                        type: 'selection',
                        align: 'center'
                    },

                    {
                        title: '模板名称',
                        key: 'printFormName'
                    },

                    {
                        title: '版本号',
                        key: 'versionList',
                        slot: 'versionList',
                    },
                    // {
                    //     title: "操作",
                    //     key: "action",
                    //     align: "center",
                    //     slot: "action",
                    //     // fixed: "right"
                    // },

                ],
                columns5: [
                    {
                        title: "--",
                        key: "action",
                        align: "center",
                    },

                    {
                        title: '模板名称',
                        key: 'printFormName'
                    },

                    {
                        title: '版本号',
                        key: 'versionList',
                        slot: 'versionList',
                    },
                    // {
                    //     title: "--",
                    //     key: "action",
                    //     align: "center",
                    // },


                ],
            };
        },
        created() {

        },
        mounted() {
            // this.verSion = 'newVersion';
            this.init();
        },
        methods: {
            init() {

                if (this.pageParams.contractVersion === "newContractVersion") {
                    this.verSion = "newVersion"
                }
                if (this.pageParams.contractVersion === "otherContractVersion") {
                    this.verSion = "otherVersion"

                }
                this.readRadio =
                    (this.pageParams.processStatus === "underReview" || this.pageParams.processStatus === "repairedUnderReview"
                        || this.pageParams.processStatus === "approved" || this.pageParams.processStatus === "refuse"
                    );
                this.formData = Object.assign({}, this.formData, this.pageParams);
                if (this.pageParams.changeSubject) {
                    this.changeMain(this.pageParams.changeSubject);
                }
                this.formData.isSP = this.isSP
                console.log("this.15313isSP",this.isSP,this.formData.isSP )
                console.log("  this.formData",  this.formData )
                this.showContract();
                this.queryAllContractByScene();

            },

            getBasicInfo(data) {
                if (!data) return '';
            },

            /*
             * formData 数据验证 验证通过后 将数据传回父级处理  (保存/提交/审核 通用)
             * @param {String} funcName 回传给父级的方法 (注意父级需要传方法)
             * @param {Object} [data]   父级带来的数据
             */
            formValidate(funcName, data) {
                if (!funcName) return;
                this.$refs.formData.validate(valid => {
                    if (valid) {
                        let d = Object.assign({}, data, this.formData, this.Obj);
                        this.$emit(funcName, d);
                    } else {
                        this.$Message.error("请填写正确的信息!");
                        this.$emit("closeSpin");
                        verifyErrorToView(this.$refs.formData.fields);
                    }
                });
            },

            rowClassName: function (row, index) {
                if (index % 2 == 0) {
                    return 'ivu-table-stripe-even';
                } else return 'ivu-table-stripe-odd';
            },
            onSelect(selection, index) {
                this.selecteds = selection;

                for (let i = 0; i < selection.length; i++) {
                    if (selection[i].versionId === null || selection[i].versionId === undefined || selection[i].versionId === '0') {
                        this.selecteds[i].versionId = selection[i].versionList[0].versionId
                        this.selecteds[i].printNode = 'printLoanApply'
                    }
                }
                console.log("this.selecteds15232132", this.selecteds);
            },
            showContract(version) {
                // if (!this.verSion) this.verSion = "newVersion"
                // console.log("version",this.verSion)
                console.log("version", version)
                this.formData.belongNo = this.formData.contractNo
                this.formData.printNode = this.formData.printNode
                this.formData.printNode = 'printLoanApply'
                console.log("this.formData.contractNo", this.formData.contractNo)
                if (version === 'otherVersion') {
                    this.verSion = 'otherVersion'
                }
                if (version === 'newVersion') {
                    this.verSion = 'newVersion'
                }
                console.log("this.verSion", this.verSion)
                if (this.verSion === 'otherVersion') {
                    this.formData.contractVersion = 'otherContractVersion';
                    queryContract(this.formData).then(res => {
                            if (res.code === "0000") {
                                // this.$Message.success("保存成功!");
                                this.otherVersionContractReissurVo = res.data
                                console.log("this.otherVersionContractReissurVo", this.otherVersionContractReissurVo)
                            }
                        }
                    )
                }
                if (this.verSion === 'newVersion') {
                    this.formData.contractVersion = 'newContractVersion';
                    queryNewContract(this.formData).then(res => {
                        if (res.code === "0000") {
                            // this.$Message.success("保存成功!");
                            this.newVersionContractReissurVoOld = res.data
                            console.log("this.newVersionContractReissurVoOld", this.newVersionContractReissurVoOld)
                        }
                    })
                }
                queryBusinessContract(this.formData).then(res => {
                    if (res.code === "0000") {
                        // this.$Message.success("保存成功!");
                        this.newVersionContractReissurVo = res.data
                        console.log("this.newVersionContractReissurVo", this.newVersionContractReissurVo)
                    }
                    this.$nextTick(()=>{
                        this.handleSelectAll(true)
                    })
                })



            },
            handleSelectAll(status) {
                this.$refs.selection.selectAll(status);
                console.log(status,"status")
            },
            queryAllContractByScene() {
                this.formData.printNode = this.formData.printNode
                this.formData.printNode = 'printLoanApply'
                console.log("this.formData", this.formData)
                queryContractByScene(this.formData).then(
                    res => {
                        if (res.code === "0000") {
                            console.log("res.data", res.data)
                            // this.$Message.success("保存成功!");
                            this.ContractReissurVo2 = res.data
                        }
                    }
                )
            },
            remove(type, index) {
                console.log(type, index, "type,index")
                if (type === 'new') {
                    console.log("index", index)

                    this.newVersionContractReissurVo.splice(index, 1)
                    console.log("this.otherVersionContractReissurVo", this.otherVersionContractReissurVo)
                }
                if (type === 'other') {
                    this.otherVersionContractReissurVo.splice(index, 1)
                }
            },

            getCheckedNodes(v) {
                this.showForm = v;
                console.log(v);
                console.log(v.children);
                if (v.length > 0) {
                    // 转换null为""
                    for (let attr in v[0]) {
                        if (v[0][attr] === null) {
                            v[0][attr] = "";
                        }
                    }
                    let str = JSON.stringify(v[0]);
                    let data = JSON.parse(str);
                    this.showForm.parentId = data.id;
                    console.log("data.children=" + data.children);
                    if (data.children === undefined) {
                        this.showForm.parentTitle = data.title;

                    } else {
                    }
                }
            },
            getOneVersionId(v) {
                console.log("select", v)

            },
            // 弹窗
            showModal() {
                this.isModalOpen = true;
                this.clockModal = false;
            },
            // 弹窗
            showModal2() {
                this.isModalOpen = true;
                this.clockModal = true;
            },
            closeModal() {
                this.isModalOpen = false;
                this.handleSelectAll(true)
            },
            addContractFile(data) {
                if (!data) return;
                const {belongNo, printFormName, versionId} = data
                let row = null;
                if (this.clockModal === true) {
                    row = this.newVersionContractReissurVo;
                } else {
                    row = this.otherVersionContractReissurVo;
                }
                row.push({
                    printNode: 'printLoanApply',
                    belongNo,
                    printFormName,
                    versionId,
                    versionList: [
                        {
                            versionId,
                        }
                    ]
                })
                this.$nextTick(()=>{
                    this.handleSelectAll(true)
                })
            },
        }
    };
</script>
<style scoped>
    .item-disabled {
        margin-bottom: 0;
    }

    .type-box {
        display: flex;
        flex-flow: row;
    }

    .item-title {
        text-align: center;
    }

    .business-btns {
        padding-bottom: 16px;
    }
</style>
